package com.wudizaba.nb.kafkaConsumer;

import config.kafkaConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Slf4j
public class consumer {

    @KafkaListener(topics = kafkaConfig.TOPIC_TEST,groupId = kafkaConfig.TOPIC_GROUP1)
    public void consume1(ConsumerRecord<?,?> record , Acknowledgment ack,@Header(KafkaHeaders.RECEIVED_TOPIC) String topic){
        Object value = record.value();
        Optional<Object> value1 = Optional.ofNullable(value);
        if (value1.isPresent()){
            Object message = value1.get();
            log.info("接收到消息:{}",message);
            log.info("接收到消息的topic:{},消息接收成功：{}",topic,message);
            ack.acknowledge();
        }
    }

    @KafkaListener(topics = kafkaConfig.TOPIC_TEST,groupId = kafkaConfig.TOPIC_GROUP2)
    public void consume2(ConsumerRecord<?,?> record , Acknowledgment ack,@Header(KafkaHeaders.RECEIVED_TOPIC) String topic){
        Object value = record.value();
        Optional<Object> value1 = Optional.ofNullable(value);
        if (value1.isPresent()){
            Object message = value1.get();
            log.info("接收到消息:{}",message);
            log.info("接收到消息的topic:{},消息接收成功：{}",topic,message);
            ack.acknowledge();
        }

    }
}
